package com.xinxing.city.recoverycheck.sdk_ttys;

import java.util.Arrays;
public class AcPackage {


    private final byte encrypt = 0x0a;                             // 加密标志(1字节)  00 不加密 01 md5 02 hex
    public byte[] start = new byte[]{(byte) 0xaa, (byte) 0xac};    // 起始标志
    public byte dev_type = 0x01;                                   // 设备类型(1字节)
    public byte[] devsn;                                           // 设备编号(8字节)  2af53b0eb9ba0d8f
    public byte frame = 0x01;                                      // 桢类型(1字节)  序列号域
    public byte[] number;                                          // 本地存储的消息id(2字节)
    public byte[] length;                                          // body体长度(2字节)
    public byte[] body;                                            // body体(n字节)
    public byte[] check;                                           // 校验码2字节
    public byte end = 0x0a;                                        // 结束域(1字节)
    public String frameName = "";                                        // 结束域(1字节)

    public AcPackage(byte[] pac) {
        if (pac == null || pac.length < 29) {
            System.out.println("报文长度错误");
            return;
        }
        decode(pac);
    }

    public AcPackage(AcPackage acp) {
        if (acp == null) {
            System.out.println("报文长度错误");
            return;
        }
        decode(acp);
    }

    public AcPackage() {

        devsn = CC.devSn;

        number = new byte[2];
        length = new byte[2];
        body = new byte[0];
        check = new byte[2];
    }

    public AcPackage(byte frame, byte[] body) {
        super();
        this.frame = frame;
        this.body = body;
    }

    public static boolean isPack(byte[] buffer) {

        if (buffer.length < 4) {
            System.out.println("校验长度不足：" + Hexs.ByteToHex(buffer));
            return false;
        }
        byte[] crcbyte = Hexs.subByte(buffer, 0, buffer.length - 3);
        byte[] check = CRC_CCITT.CCITT_true_Byte(crcbyte);
        byte[] check2 = new byte[]{buffer[buffer.length - 3], buffer[buffer.length - 2]};
        return Arrays.equals(check, check2);
    }

    //aaac01000000000000000000000000000000000000090000010002931f0a
    public static boolean isPackage(byte[] in) {
        if (in != null && in.length > 29) {
            if (in.length > 65530) return false;
            return in[0] == (byte) 0xaa && in[1] == (byte) 0xac && in[in.length - 1] == (byte) 0x0a;
        }
        return false;
    }

    public byte[] getPackage() {
        number = new byte[]{0x00, 0x00};
        byte[] tes = new byte[]{0x00, 0x00};

        if (devsn == null || devsn.length < 1 || Arrays.equals(devsn, tes)) {
            devsn = CC.devSn;
        } else {
            devsn = CC.devSn;
        }

        // aaac 01 00000000000000f7e3c91c017303 35 0000 02 000100 23f7 0a


        length = Hexs.LowIntToByte2F(body.length);
        byte[] CRC0 = Hexs.byteMerger(start, new byte[]{dev_type}, devsn, new byte[]{frame}, number, length, body);
        check = CRC_CCITT.CCITT_true_Byte(CRC0);

        byte[] out = Hexs.byteMerger(start, new byte[]{dev_type}, devsn, new byte[]{frame}, number, length, body, check, new byte[]{end});
        return out;
    }

    private void decode(byte[] in) {

        if (in == null || in.length < 29) {
            System.err.println("报文长度错误");
            return;
        }
        if (isPackage(in)) {

            int i = 0;
            start = new byte[]{in[0], in[1]};
            i += 2;
            dev_type = in[i];
            i += 1;
            devsn = Hexs.subByte(in, i, 18);
            CC.devSn = devsn;

            i += 18;
            frame = in[i];
            i += 1;
            number = new byte[]{in[i], in[i + 1]};
            i += 2;
            length = new byte[]{in[i], in[i + 1]};
            i += 2;
            body = Hexs.subByte(in, i, in.length - i - 3);
            check = new byte[]{in[in.length - 3], in[in.length - 2]};
            end = in[in.length - 1];
        } else {
            System.err.println(" 报文校验错误：" + Hexs.ByteToHex(in));
        }

    }

    private void decode(AcPackage acp) {

        if (acp == null) {
            System.out.println("报文长度错误");
            return;
        }
        start = acp.start;
        dev_type = acp.dev_type;
        devsn = acp.devsn;
        frame = acp.frame;
        number = acp.number;
        length = acp.length;
        end = acp.end;
        frameName = "";



    }


}
